﻿using System;
using System.Web.Security;
using System.Web.UI.WebControls;

namespace WebApplication1.UserRolerManagement
{
    public partial class UserManagement : System.Web.UI.Page
    {
        public string seachFactor
        {
            get { return ViewState["SeachFactor"].ToString(); }
            set { ViewState["SeachFactor"] = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                seachFactor = string.Empty;
                BindUsers();
                BindRoles();
            }
        }

        protected void Button5_Click(object sender, EventArgs e)
        {
            seachFactor = this.TextBox1.Text.Trim();
            BindUsers();
        }

        protected void Button6_Click(object sender, EventArgs e)
        {
            seachFactor = string.Empty;
            BindUsers();
        }

        protected void BindRoles()
        {
            GridView2.DataSource = Roles.GetAllRoles();
            GridView2.DataBind();
        }

        protected void BindUsers()
        {
            if (string.IsNullOrEmpty(seachFactor))
            {
                GridView1.DataSource = Membership.GetAllUsers();
                GridView1.DataBind();
            }
            else
            {
                GridView1.DataSource = Membership.FindUsersByName(seachFactor);
                GridView1.DataBind();
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (GridView1.SelectedIndex < 0)
            {
                Label1.Text = "没有选中有效的用户";
                Label1.Visible = true;
            }
            else if (GridView2.SelectedIndex < 0)
            {
                Label1.Text = "没有选中有效的角色";
                Label1.Visible = true;
            }

            else if (Roles.IsUserInRole(GridView1.SelectedRow.Cells[1].Text.ToString(), GridView2.SelectedRow.Cells[1].Text.ToString()))
            {
                Label1.Text = "用户：" + GridView1.SelectedRow.Cells[1].Text.ToString() + "已经在角色：" + GridView2.SelectedRow.Cells[1].Text.ToString() + "中";
                Label1.Visible = true;
            }
            else
            {
                Roles.AddUserToRole(GridView1.SelectedRow.Cells[1].Text.ToString(), GridView2.SelectedRow.Cells[1].Text.ToString());
                Label1.Text = "配置成功：用户：" + GridView1.SelectedRow.Cells[1].Text.ToString() + "已经成为角色：" + GridView2.SelectedRow.Cells[1].Text.ToString() + "中一员";
                Label1.Visible = true;
            }
        }


        protected void Button2_Click(object sender, EventArgs e)
        {
            if (GridView1.SelectedIndex < 0)
            {
                Label2.Text = "没有选中有效的用户";
                Label2.Visible = true;
            }
            else if (GridView2.SelectedIndex < 0)
            {
                Label2.Text = "没有选中有效的角色";
                Label2.Visible = true;
            }
            else if (!Roles.IsUserInRole(GridView1.SelectedRow.Cells[1].Text.ToString(), GridView2.SelectedRow.Cells[1].Text.ToString()))
            {
                Label2.Text = "用户：" + GridView1.SelectedRow.Cells[1].Text.ToString() + "并不在角色：" + GridView2.SelectedRow.Cells[1].Text.ToString() + "中";
                Label2.Visible = true;
            }
            else
            {
                Roles.RemoveUserFromRole(GridView1.SelectedRow.Cells[1].Text.ToString(), GridView2.SelectedRow.Cells[1].Text.ToString());
                Label2.Text = "删除成功：用户：" + GridView1.SelectedRow.Cells[1].Text.ToString() + "已经从角色：" + GridView2.SelectedRow.Cells[1].Text.ToString() + "中删除";
                Label2.Visible = true;
            }
        }
        protected void Button3_Click(object sender, EventArgs e)
        {
            if (GridView1.SelectedIndex < 0)
            {
                Label3.Text = "没有选中有效的用户";
                Label3.Visible = true;
            }
            else
            {
                GridView3.DataSource = Roles.GetRolesForUser(GridView1.SelectedRow.Cells[1].Text.ToString());
                GridView3.DataBind();
                GridView3.Visible = true;
                Label3.Visible = false;
            }
        }
        protected void Button4_Click(object sender, EventArgs e)
        {
            if (GridView2.SelectedIndex < 0)
            {
                Label4.Text = "没有选中有效的角色";
                Label4.Visible = true;
            }
            else
            {
                BindUsersforRole();
            }
        }

        protected void BindUsersforRole()
        {
            GridView4.DataSource = Roles.GetUsersInRole(GridView2.SelectedRow.Cells[1].Text.ToString());
            GridView4.DataBind();
            GridView4.Visible = true;
            Label4.Visible = false;
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.GridView1.PageIndex = e.NewPageIndex;
            BindUsers();
        }

        protected void GridView4_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.GridView4.PageIndex = e.NewPageIndex;
            BindUsersforRole();
        }
    }
}
